<include file="header"/>
<body>
<div class="wraper">
    <div class="load">
        <img class="load-bee" src="/assets/base/repair/images/load-bee.png"/>
    </div>
    <div class="top">
        <p>
            <a href="javascript:history.back(-1)" class="iconfont icon-zuo fl-left"></a>
            <if condition="$picked neq 1 and $userType neq 1">
                地址管理
                <else/>
                联系方式管理
            </if>
            <a href="{BEESCRM::U('base/center/center',array('account_id'=>$account_id))}"
               class="iconfont icon-shouye fl-right"></a>
        </p>
    </div>
    <if condition="($accountId neq 12) and ($userType eq 0)" >
        <p class="title">生活区选择</p>

        <p class="form-box">
            <select class="select" id="community">
                <option value="0">生活区选择</option>
                <volist name="community" id="vo">
                    <option value="{BEESCRM:$vo.community_id}"
                    <if condition="$data['community_id'] eq $vo['community_id']"> selected="selected"</if>
                    >{BEESCRM:$vo.community_name}</option>
                </volist>
            </select>
            <span class="triangle"></span>
        </p>
        <p class="title">楼幢选择</p>

        <p class="form-box">
            <select class="select" id="building">
                <if condition="$data['building_id']">
                    <volist name="building" id="vo">
                        <option value="{BEESCRM:$vo['building_id']}"
                        <if condition="$data['building_id'] eq $vo['building_id']">selected="selected"</if>
                        >{BEESCRM:$vo['building_name']}</option>
                    </volist>
                    <else/>
                    <option value="0">请选择楼幢</option>
                </if>
            </select>
            <span class="triangle"></span>
        </p>
        <p class="title">寝室号</p>

        <p class="form-box">
            <input class="input" value="{BEESCRM:$data.room_name}" id="room"/>
        </p>
    </if>

    <p class="title">姓名</p>
    <p class="form-box">
        <input class="input" value="{BEESCRM:$data.real_name}" id="real_name"/>
    </p>
    <p class="title">联系方式</p>
    <p class="form-box">
        <input class="input" value="{BEESCRM:$data.mobile}" id="mobile"/>
    </p>

    <if condition="($accountId eq 28) and ($picked eq 0)">
        <p class="title">详细地址</p>
        <p class="form-box">
            <input class="input" value="{BEESCRM:$data.address_detail}" id="addressDetail"/>
        </p>
    <elseif condition="$userType neq 0" />
        <p class="title">详细地址</p>
        <p class="form-box">
            <input class="input" value="{BEESCRM:$data.address_detail}" id="addressDetail"/>
        </p>
    </if>

    <div class="marginb-50"></div>
    <div class="btn-box">
        <input type="hidden" id="address_id" value="{BEESCRM:$address_id}">
        <input type="hidden" id="picked" value="{BEESCRM:$picked}">
        <input type="hidden" id="user_type_container" value="{BEESCRM:$userType}" />
        <input type="hidden" id="account_id_container" value="{BEESCRM:$accountId}" />
        <a href="javascript:void(0)" class="btn big" id="addAddr">提交</a>
    </div>
</div>

<include file="commonJS"/>
<if condition="$is_repair eq 1">
<script src="/assets/base/common/ajaxgetroominfo_one.js"></script>
<else/>
<script src="/assets/base/common/ajaxgetroominfo.js"></script>
</if>
<script>
    $(function () {
        //调用房间联动js
        ajaxgetroominfo(true, false, false);
        $('.list-box').on('click', '.list', function () {
            $(this).addClass('check').siblings().removeClass('check');
        })
    });

    $('#addAddr').on('click', function () {
        /*var order_id=$('#order_id').val();*/
        var address_id = $('#address_id').val();
        var real_name = $('#real_name').val();
        var mobile = $('#mobile').val();
        var verify_method = /^1[3|4|5|7|8][0-9]{9}$/;
        var room = $('#room').val();
        var community_id = $("#community").val();
        var community_name = $("#community").find("option:selected").text();
        var building_id = $("#building").val();
        var building_name = $("#building").find("option:selected").text();
        var picked = $('#picked').val();
        var userType = $('#user_type_container').val();

        var address_detail = $("#addressDetail").val();
        if (picked != 1 && userType == 0) {
            if (!community_id || community_id == '0') {
                $.alert('error', '请选择生活区');
                return false;
            } else if (!building_id || building_id == '0') {
                $.alert('error', '请选择寝室楼');
                return false;
            } else if (!room) {
                $.alert('error', '请选择寝室号');
                return false;
            } else if (real_name == '') {
                $.alert('error', '请输入姓名');
                return false;
            } else if (!verify_method.test(mobile)) {
                $.alert('error', '请输入正确的手机号');
                return false;
            } else if(userType != 0 && (address_detail == '0' || !address_detail) ) {
                $.alert('error', '请输入详细地址');
                return false;
            }else {
                var data = {
                    'address_id': address_id,
                    'real_name': real_name,
                    'mobile': mobile,
                    'community_id': community_id,
                    'community_name': community_name,
                    'building_id': building_id,
                    'building_name': building_name,
                    'room_name': room,
                    'picked': picked,
                    'address_detail':address_detail
                };
                $.ajax({
                    url: "/base/address/addAddress",
                    type: 'post',
                    data: data,
                    dataType: 'json',
                    success: function (data) {
                        if (data.status == 'S') {
                            $.alert('success', data.message);
                            setTimeout("location.href ='/base/address/address/picked/" + data.picked + "'", 2000);
                        } else {
                            $.alert('error', data.message);
                            return false;
                        }
                    }
                });
            }
        } else {
            if (!real_name || real_name == '') {
                $.alert('error', '请输入姓名');
                return false;
            }

            if(!mobile || mobile == ''){
                $.alert('error', '请输入手机号');
                return false;
            }

            if (!verify_method.test(mobile)) {
                $.alert('error', '请输入正确的手机号');
                return false;
            }
            if(userType != 0 && (address_detail == '0' || !address_detail) ){
                $.alert('error', '请输入详细地址');
                return false; 
            }

            var data = {
                'address_id': address_id,
                'real_name': real_name,
                'mobile': mobile,
                'picked': picked,
                'userType' : userType,
                'address_detail' : address_detail
            };
            $.ajax({
                url: "/base/address/addAddress",
                type: 'post',
                data: data,
                dataType: 'json',
                success: function (data) {
                    if (data.status == 'S') {
                        $.alert('success', data.message);
                        setTimeout("location.href ='/base/address/address/picked/" + data.picked + "'", 2000);
                    } else {
                        $.alert('error', data.message);
                        return false;
                    }
                }
            });
        }
    });
</script>
</body>

</html>
